<template>
  <!-- 头部导航栏 -->
  <div id="nav-bar">
    <!-- 头部导航栏主体内容 -->
    <div class="nav-bar-content">
      <!-- logo内容 -->
      <div class="logo_box">
        <a hidefocus="true" href="/">网易云音乐</a>
      </div>
      <!-- 导航路由内容 -->
      <div class="router_box">
        <div
          class="box_item"
          v-for="item in navBarRouters"
          :key="item.value"
          :class="{ active: current === item.value }"
          @click="navBarItemClick(item)"
        >
          {{ item.key }}
        </div>
      </div>
      <!-- 搜索框内容 -->
      <div class="search_box">
        <i class="icon"></i>
        <input type="text" class="search" />
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, reactive, toRefs, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
export default defineComponent({
  name: "NavBar",
  setup() {
    // 定义数据
    let data = reactive({
      // 定义导航中的路由数据
      navBarRouters: [
        {
          key: "推 荐",
          value: "/recommend",
        },
        {
          key: "排行榜",
          value: "/rankinglist",
        },
        {
          key: "歌 单",
          value: "/songlist",
        },
        {
          key: "歌 手",
          value: "/singer",
        },
        {
          key: "新碟上架",
          value: "/newdisc",
        },
      ],
      // 获取当前的活跃的导航
      current: "/recommend",
    });
    const router = useRouter();

    // 点击导航栏事件
    const navBarItemClick = (item) => {
      // item是一个proxy对象
      data.current = item.value;
      // 跳转对应的路由
      router.push(item.value);
    };

    onMounted(() => {
      handleRouterChange();
    });

    // 监听当前路由变化
    watch(
      () => router.currentRoute.value,
      () => {
        handleRouterChange();
      }
    );

    // 路由变化后的事件处理
    let handleRouterChange = () => {
      let path = router.options.history.state.current;
      if (path == "/") {
        data.current = "/recommend";
        return;
      }
      if (path.indexOf("/newdisc") != -1) {
        data.current = "/newdisc";
        return;
      }
      data.current = path;
    };

    return {
      ...toRefs(data),
      navBarItemClick,
    };
  },
});
</script>
<style scoped>
#nav-bar {
  width: 100%;
  height: 80px;
  background-color: #242424;
  border-bottom: 2px solid #c20c0c;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}
.nav-bar-content {
  width: 70%;
  height: 100%;
  margin: 0 auto;

  display: flex;
  align-items: center;
}
.logo_box {
  width: 170px;
  height: 70px;
  background: url("../../assets/img/topbar.png") no-repeat;
  background-size: 0px 15px 162px 40px;
  cursor: pointer;
}
.logo_box a {
  display: inline-block;
  width: 100%;
  height: 100%;
  color: #242424;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.router_box {
  height: 80px;
  display: flex;
  align-items: center;

  color: #ccc;
  font-size: 15px;
}
.box_item {
  height: 80px;
  padding: 0 30px;
  cursor: pointer;
  transition: all 0.3s;
  line-height: 80px;
}
.box_item:hover {
  color: #ffffff;
  background-color: #000000;
}

.active {
  color: #ffffff;
  background-color: #000000;
}
.search_box {
  position: relative;
  width: 360px;
  height: 30px;
  margin-left: 10px;
}
.search {
  width: 100%;
  height: 30px;
  border-radius: 12px;
  padding-left: 30px;
  color: #555;
  font-size: 14px;
}
.icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #666;
}
</style>
